home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / lib2to3 / fixes / fix_itertools_imports.py < prev    next >
Encoding:
Python Source  |  2009-04-18  |  1.6 KB  |  46 lines

  1. """ Fixer for imports of itertools.(imap|ifilter|izip|ifilterfalse) """
  2.  
  3. # Local imports
  4. from .. import fixer_base
  5. from ..fixer_util import BlankLine
  6.  
  7. class FixItertoolsImports(fixer_base.BaseFix):
  8.     PATTERN = """
  9.               import_from< 'from' 'itertools' 'import' imports=any >
  10.               """ %(locals())
  11.  
  12.     def transform(self, node, results):
  13.         imports = results['imports']
  14.         children = imports.children[:] or [imports]
  15.         for child in children:
  16.             if not hasattr(child, 'value'):
  17.                 # Handle 'import ... as ...'
  18.                 continue
  19.             if child.value in ('imap', 'izip', 'ifilter'):
  20.                 # The value must be set to none in case child == import,
  21.                 # so that the test for empty imports will work out
  22.                 child.value = None
  23.                 child.remove()
  24.             elif child.value == 'ifilterfalse':
  25.                 node.changed()
  26.                 child.value = 'filterfalse'
  27.  
  28.         # Make sure the import statement is still sane
  29.         children = imports.children[:] or [imports]
  30.         remove_comma = True
  31.         for child in children:
  32.             if remove_comma and getattr(child, 'value', None) == ',':
  33.                 child.remove()
  34.             else:
  35.                 remove_comma ^= True
  36.  
  37.         if unicode(children[-1]) == ',':
  38.             children[-1].remove()
  39.  
  40.         # If there are no imports left, just get rid of the entire statement
  41.         if not (imports.children or getattr(imports, 'value', None)):
  42.             p = node.get_prefix()
  43.             node = BlankLine()
  44.             node.prefix = p
  45.         return node
  46.